<template>
	<div>
		新闻编辑
	</div>
	<div>
		<el-input v-model="news.title" placeholder="请输入标题"></el-input>
	</div>
	<div>
		<el-input type="textarea" :rows="20" placeholder="请输入内容" v-model="news.content"></el-input>
	</div>
	<div>
		<el-select v-model="news.category" placeholder="请选择分类">
			<el-option label="国内" value="国内"></el-option>
			<el-option label="军事" value="军事"></el-option>
			<el-option label="生活" value="生活"></el-option>
			<el-option label="汽车" value="汽车"></el-option>
			<el-option label="互联网" value="互联网"></el-option>
			<el-option label="金融" value="金融"></el-option>
		</el-select>
	</div>
	<div>
		<el-input v-model="news.author" placeholder="请输入作者"></el-input>
	</div>
	<div>
		<el-button>取消</el-button>
		<el-button @click="save()">保存</el-button>
	</div>
</template>

<script>
	import axios from 'axios'
	axios.defaults.baseURL='/api'
	import { ElMessage } from 'element-plus'
	export default {
		data() {
			return {
				news: {
					id:0,
					title: '',
					category: '',
					pubTime: '',
					author: '',
					readTimes: 0,
					content: '',
				}
			}
		},
		methods:{
			//保存按钮点击事件
			save(){
				//调用后台接口保存新闻
				axios.put('news',this.news).then(response => {
					if(response.data){
						ElMessage.success('保存成功！')
					}else{
						ElMessage.success('保存失败！')
					}
					this.$router.push('/newsList')
				})
			}
		},
		mounted() {
			//加载详情
			axios.get('news/findOne',{
				params:{
					id:this.$route.params.id
				}
			}).then(response => {
				//console.log(response.data)
				this.news = response.data
			})
		}
	}
</script>

<style>
</style>